<?php 
if(KT!='KhiThe') exit;
class users_controller extends users_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
		parent::addObject('khithe_paging');
	}
	
	public function index_action(){
			
		$limit=25;
		$totalRows = mysql_num_rows(parent::getUsers('',''));
		$strPaging = $this->display_paging($totalRows, intval($this->valUrl(1)), intval($limit), '/admin/users/index', $this->lang,'/');
		$this->lang['paging'] = $strPaging;
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultHtml = $this->view_users($limit);
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		
		return $strHtmlContent;
	}
	
	public function add_action(){
		
		$this->lang['sms'] ='';
		if($_GET['iframe']=='true'){
			if(isset($_POST['txt_users']))
			{
				/* action PROCESS */	
				$Username = parent::checkStrMySQL($_POST['txt_users']);
				$Email = parent::checkStrMySQL($_POST['txt_Email']);
				if($Username==''){
					$this->lang['sms'] = $this->lang['EmptyUser'];
					
				}else if(parent::getUsername($Username)>0) {
					$this->lang['sms'] = $this->lang['YesUser'];
				
				}elseif(parent::getMail($Email)>0) {
					$this->lang['sms'] = $this->lang['YesMail'];
				
				}else{
					$done = parent::updateInsertUser($_POST);
					if($done) $this->gotowinparent(url_base.'admin/users');
				}
			}
			
			/* VIEW FROM */		
			$this->lang['txt_usersValue'] = isset($_POST['txt_users'])?$_POST['txt_users']:NULL;
			$this->lang['txt_passValue'] ='';
			$this->lang['txt_pass2Value'] ='';
			$mod = isset($_POST['mod'])?$_POST['mod']:NULL;
			$this->lang['select_Admin'] =($mod=='Admin'?' selected="selected" ':'');
			$this->lang['select_Mod'] =($mod=='Members'?' selected="selected" ':'');
			$this->lang['select_Cus'] =($mod=='Customer'?' selected="selected" ':'');
			$this->lang['txt_FullnameValue'] = isset($_POST['txt_Fullname'])?$_POST['txt_Fullname']:NULL;
			$this->lang['txt_PhoneValue'] = isset($_POST['txt_Phone'])?$_POST['txt_Phone']:NULL;
			$this->lang['txt_EmailValue'] = isset($_POST['txt_Email'])?$_POST['txt_Email']:NULL;
			$this->lang['txt_YahooValue'] = isset($_POST['txt_Yahoo'])?$_POST['txt_Yahoo']:NULL;
			$this->lang['txt_MobiValue'] = isset($_POST['txt_Mobi'])?$_POST['txt_Mobi']:NULL;
			$this->lang['txt_CMNDValue'] = isset($_POST['txt_CMND'])?$_POST['txt_CMND']:NULL;
			$this->lang['txt_AddressValue'] = isset($_POST['txt_Address'])?$_POST['txt_Address']:NULL;
			
			$this->lang['active'] ='add';
			$this->lang['usersID'] ='';
			$this->lang['titlePageEditor'] = $this->lang['addTitle'];
			$strHtmlContent = $this->khithe_display('editor');
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			
		}
		
		return $strHtmlContent;
	}
	
	public function edit_action(){
		$this->lang['sms'] ='';
		if($_GET['iframe']=='true'){
			if(isset($_POST['txt_users']))
			{
				/* action PROCESS */
				$Username = parent::checkStrMySQL($_POST['txt_users']);
				$user_old = parent::checkStrMySQL($_POST['user_old']);
				$Email = parent::checkStrMySQL($_POST['txt_Email']);
				$mail_old = parent::checkStrMySQL($_POST['mail_old']);
				
				if($Username==''){
					$this->lang['sms'] = $this->lang['EmptyUser'];
					
				}else if($user_old!=$Username && parent::getUsername($Username)>0) {
					$this->lang['sms'] = $this->lang['YesUser'];
				
				}elseif($mail_old!=$Email && parent::getMail($Email)>0) {
					$this->lang['sms'] = $this->lang['YesMail'];
				
				}else{
					$done = parent::updateInsertUser($_POST);
					if($done) parent::gotowinparent(url_base.'admin/users');
				}
			}
			
			/* VIEW FROM */
			$mod = isset($_POST['mod'])?$_POST['mod']:NULL;
			$resultUser = parent::getUserID(intval($this->valUrl(1)));
			$row_Users = mysql_fetch_assoc($resultUser);
			$strHtmlContent = $this->khithe_display('editor');
			$this->lang['titlePageEditor'] = $this->lang['editTitle'];
			
			$this->lang['txt_usersValue'] = isset($_POST['txt_users']) && $_POST['txt_users']!=''?$_POST['txt_users']:$row_Users['Username'];
			$this->lang['txt_passValue'] = '';
			$this->lang['txt_pass2Value'] = '';
			$mod = isset($_POST['mod'])?$_POST['mod']:NULL;
			$this->lang['select_Admin'] =($row_Users['Mod']=='Admin' || $mod=='Admin'?' selected="selected" ':'');
			$this->lang['select_Mod'] =($row_Users['Mod']=='Members' || $mod=='Members'?' selected="selected" ':'');
			$this->lang['select_Cus'] =($row_Users['Mod']=='Customer'|| $mod=='Customer'?' selected="selected" ':'');
			$this->lang['txt_FullnameValue'] = isset($_POST['txt_Fullname']) && $_POST['txt_Fullname']!=''?$_POST['txt_Fullname']:$row_Users['Fullname'];
			$this->lang['txt_PhoneValue'] = isset($_POST['txt_Phone']) && $_POST['txt_Phone']!=''?$_POST['txt_Phone']:$row_Users['Phone'];
			$this->lang['txt_EmailValue'] = isset($_POST['txt_Email']) && $_POST['txt_Email']!=''?$_POST['txt_Email']:$row_Users['Email'];
			$this->lang['txt_YahooValue'] = isset($_POST['txt_Yahoo']) && $_POST['txt_Yahoo']!=''?$_POST['txt_Yahoo']:$row_Users['YahooChat'].'@yahoo.com';
			$this->lang['txt_MobiValue'] = isset($_POST['txt_Mobi']) && $_POST['txt_Mobi']!=''?$_POST['txt_Mobi']:$row_Users['Mobi'];
			$this->lang['txt_CMNDValue'] = isset($_POST['txt_CMND']) && $_POST['txt_CMND']!=''?$_POST['txt_CMND']:$row_Users['CMND'];
			$this->lang['txt_AddressValue'] = isset($_POST['txt_Address']) && $_POST['txt_Address']!=''?$_POST['txt_Address']:$row_Users['Address'];
			$this->lang['active'] ='edit';
			$this->lang['usersID'] =$row_Users['ID'];
			
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		}
		
		return $strHtmlContent;
	}
	
	public function index_ajax_action(){
		
		
		////////////////users///////////////
		switch ($this->valUrl(1))
		{
			case 'open':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$select = parent::getUserID($ID);
					while($row = mysql_fetch_assoc($select)){
						if($row['Status']=='false'){
							$done = parent::setCheckedUser($ID,'true');
						}
					}
				}
			break;
			
			case 'close':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$select = parent::getUserID($ID);
					while($row = mysql_fetch_assoc($select)){
						if($row['Status']=='true'){
							$done = parent::setCheckedUser($ID,'false');
						}
					}
				}
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(2));
				foreach ( $arr as $ID ){
					$done = parent::delCheckedUser($ID);	
				}
			break;
			
		}
		if(isset($done)){
			return $this->index_action();
			
		}else{
			return 'notOK';
		}
		
	}
	
	/*Not Action*/
	function view_users($limit)
	{		
		$resultHtml ='';
		$resultUser = parent::getUsers(intval($this->valUrl(1)), intval($limit));
		while( $row_Users = mysql_fetch_assoc($resultUser) )
		{
			$arrUser = array(
				"cell_ID"=>$row_Users['ID'],
				"cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_Users['ID'].'" />',
				"cell_2"=>$row_Users['Username'],
				"cell_3"=>$row_Users['Fullname'],
				"cell_4"=>$row_Users['Email'],
				"cell_5"=>$row_Users['Address'],
				"cell_6"=>$row_Users['Mobi'],
				"class_cell"=>$row_Users['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
				
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['users'],$arrUser);
			
		} mysql_free_result($resultUser);
		
		return $resultHtml;
	}
	


}
?>